package cn.neusoft.service.impl;

import cn.neusoft.po.Dicdetail;
import cn.neusoft.mapper.DicdetailMapper;
import cn.neusoft.po.Dictype;
import cn.neusoft.query.DicdetailQuery;
import cn.neusoft.service.IDicdetailService;
import cn.neusoft.service.IDictypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hxj
 * @since 2023-12-25
 */
@Service
public class DicdetailServiceImpl extends ServiceImpl<DicdetailMapper, Dicdetail> implements IDicdetailService {

    @Override
    public Page<Dicdetail> getByPage(DicdetailQuery query) {
        //存放搜索条件
        QueryWrapper<Dicdetail> queryWrapper = Wrappers.query(new Dicdetail())
                //StringUtils.hasText(query.getUsername())  StringUtils字符串工具类  判断变量是否有值
                //三个参数：该条件加不加 boolean值 ,  比对的字段名称(跟数据库一致) ,   比对的值
                .like(StringUtils.hasText(query.getName()),"name",query.getName())
                .like(query.getTypeId()!=null,"types_id",query.getTypeId())
                .orderByDesc("id");

        //存放分页条件
        Page pageQuery = new Page(query.getCurrent(),query.getSize());

        //自定义分页时  准备好分页条件Page  准备好搜索条件Wrapper
        Page<Dicdetail> dicdetailIPage = baseMapper.selectJoin(pageQuery, queryWrapper);
        return dicdetailIPage;
    }

    @Autowired
    IDictypeService iDictypeService;

    @Override
    public List<Dicdetail> getByTypes(String sn) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id");
        queryWrapper.eq("sn",sn);
        Dictype dictype = iDictypeService.getOne(queryWrapper);

        queryWrapper = new QueryWrapper();
        queryWrapper.eq("types_id",dictype.getId());
        return list(queryWrapper);

    }
}
